﻿@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "分片条码";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }

        .layui-table-cell {
            height: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.UsageState}}：</label>
                        <div class="layui-input-inline">
                            <select id="selStatus" lay-filter="selStatus">
                                <option value="-1">所有状态</option>
                                <option value="@((int)DistributeCodeStatus.UnPrinted)">未打印</option>
                                <option value="@((int)DistributeCodeStatus.Printed)">已打印</option>
                                <option value="@((int)DistributeCodeStatus.Produced)">已生产</option>
                                <option value="@((int)DistributeCodeStatus.Recycled)">已回收</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.BarCode}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtCode" placeholder="请输入条码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="operateCodes(0,0,1,0);">
                            <i class="layui-icon layuiadmin-button-btn">&#xe66d;</i>{{lang.Field.Print}}
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
        <iframe src="~/StartUpTools.html" style="display:none;"></iframe>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        var distributeTaskId = parseInt("@ViewBag.DistributeTaskId");
        var checkedCodeIds = new Array();

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/WorkOrder/QueryDistributeCodes'
                    , method: "post"
                    , where: { distributeTaskId: distributeTaskId, status: "", code: "" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50, 100, 200]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { type: 'checkbox' }
                        , { title: '序号', type: "numbers", width: 60 }
                        , { field: 'Code', title: '条码', minWidth: 200 }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 200 }
                        , { field: 'OrderNo', title: '订单号', minWidth: 150 }
                        , { field: 'CNStatus', title: '状态', minWidth: 80 }
                        , {
                            title: '操作', minWidth: 100, fixed: 'right', templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="operateCodes(0,' + d.PkId + ',1,0)"><i class="layui-icon">&#xe66d;</i>打印</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.code = $("#txtCode").val();
                    options.where.status = $("#selStatus").val() == "-1" ? null : parseInt($("#selStatus").val());

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function operateCodes(operateType, codeId, operateAll, forceOperate) {
            if ((operateType == 0 || operateType == 1) && !window.parent.parent.getConnectionStatus()) {
                layui.layer.msg("本地工具未连接，无法打印！");
                return;
            }
            var codeIds = new Array();
            if (codeId > 0) {
                codeIds.push(codeId);
            } else {
                var checkedData = layui.table.checkStatus('table').data;
                for (var i = 0; i < checkedData.length; i++) {
                    codeIds.push(checkedData[i].PkId);
                }
            }
            if (codeIds.length <= 0) {
                layer.msg("请至少选择一条数据", { icon: 5 });
                return;
            }

            var loadId = layer.load();
            $.ajax({
                url: "/WorkOrder/OperateDistributeCodes",
                data: {
                    operateType: operateType,
                    codeIds: codeIds,
                    operateAll: operateAll == 1,
                    forceOperate: forceOperate == 1
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        if (res.ErrCode != 102004 && res.ErrCode != 102006) {
                            layer.alert(res.ErrMsg, { icon: 5 });
                        } else {
                            var btns = operateType >= 2 ? ['仅回收未打印的', '回收全部', '取消'] : ['仅打印未打印的', '打印全部', '取消'];
                            layer.confirm(res.ErrMsg, {
                                icon: 5, title: '提示',
                                btn: btns //可以无限个按钮
                                , btn3: function (index, layero) {

                                }
                            }, function (index, layero) {
                                operateCodes(operateType + 1, codeId, 0, 1);
                            }, function (index) {
                                operateCodes(operateType + 1, codeId, 1, 1);
                            });
                        }
                        return;
                    }
                    layer.msg("操作成功！");
                    $("#btnSearch").click();
                    if (operateType == 0 || operateType == 1) {
                        window.parent.layui.layer.open({
                            type: 1,
                            content: '<div style="padding: 50px;">后台正在生成标签清单，成功后将自动打印，请稍后...</div>',
                        });
                        window.parent.parent.sendMessage(JSON.stringify({
                            Command: "PrintCodes",
                            Body: {
                                TaskId: res.Data.TaskId
                            }
                        }));
                    }
                }
            });
        }
    </script>
}
